package myTCPTest2;


import java.io.*;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Properties;
import java.util.UUID;

public class ServerDemo {
    public static void main(String[] args) throws Exception {
        // method();

        // method2();

        // method3();

        // method4();

        InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("prop.properties");

        Properties prop = new Properties();
        prop.load(is);
        is.close();

        // 下面开始使用反射创建对象并调用方法

        // 获取字节码文件对象
        Class cla = Class.forName(prop.getProperty("className"));
        // 获取构造器对象
        Constructor constructor = cla.getConstructor();
        // 利用构造器对象创建一个对象
        Object o = constructor.newInstance();
        // 获取方法对象
        Method method = cla.getMethod(prop.getProperty("methodName"), String.class);
        // 运行方法
        method.invoke(o, "vv");
    }

    private static void method4() throws IOException {
        ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader();

        // 利用加载器加载一个指定的文件
        InputStream is = systemClassLoader.getResourceAsStream("prop.properties");

        Properties prop = new Properties();
        prop.load(is);

        System.out.println(prop);

        assert is != null;
        is.close();
    }

    private static void method3() {
        // 获取系统加载器
        ClassLoader classLoader = ClassLoader.getSystemClassLoader();

        // 获取系统加载器的父类加载器
        ClassLoader classLoader1 = classLoader.getParent();

        // 获取平台加载器的父加载器
        ClassLoader classLoader2 = classLoader1.getParent();

        System.out.println("系统类加载器" + classLoader);
        System.out.println("平台类加载器" + classLoader1);
        System.out.println("启动类加载器" + classLoader2);
    }

    private static void method2() {
        // 创建文件名称随机且唯一
        UUID uname = UUID.randomUUID();
        System.out.println(uname.toString());
    }

    private static void method() throws IOException {
        // 创建服务器端的Socket对象
        ServerSocket ss = new ServerSocket(12346);

        // 侦听客户端，得到一个Socket对象
        Socket accept = ss.accept();

        // 获取输入流
        InputStream is = accept.getInputStream();
        InputStreamReader isr = new InputStreamReader(is);
        BufferedReader br = new BufferedReader(isr);

        String line;
        while((line = br.readLine()) != null){
            System.out.println(line);
        }

        // 释放资源
        ss.close();
    }
}
